//
//  FBtMessageModel+Model.m
//  JwPack
//
//  Created by 陈警卫 on 2025/7/30.
//

#import "FBtMessageModel+Model.h"

@implementation FBtMessageModel (Model)

+ (FBtMessageModel *)modelWithSignal:(FBtSig)signal value:(NSData *)value {
    FBtMessageModel *messageModel = [[FBtMessageModel alloc] init];
    messageModel.signal = signal;
    messageModel.valueData = value;
    [messageModel setupCompleData];
    return messageModel;
}

/// 配网
+ (FBtMessageModel *)model_setNetWithSsid:(NSString *)ssid pwd:(NSString *)pwd {
    
    UInt8 ssidLength = ssid.length;
    NSData *ssidLengthData = [NSData dataWithBytes:&ssidLength length:sizeof(ssidLength)];
    NSData *ssidData = [ssid dataUsingEncoding:(NSUTF8StringEncoding)];
    
    UInt8 pwdLength = pwd.length;
    NSData *pwdLengthData = [NSData dataWithBytes:&pwdLength length:sizeof(pwdLength)];
    NSData *pwdData = [pwd dataUsingEncoding:(NSUTF8StringEncoding)];
    
    NSMutableData *value = [NSMutableData data];
    [value appendData:ssidLengthData];
    [value appendData:ssidData];
    [value appendData:pwdLengthData];
    [value appendData:pwdData];
    FBtMessageModel *messageModel = [self modelWithSignal:(FBtSig_setNet) value:value];
    return messageModel;
}

/// 微逆配网
+ (FBtMessageModel *)model_setNetMSWithSsid:(NSString *)ssid pwd:(NSString *)pwd {
    
    UInt8 ssidLength = ssid.length;
    NSData *ssidLengthData = [NSData dataWithBytes:&ssidLength length:sizeof(ssidLength)];
    NSData *ssidData = [ssid dataUsingEncoding:(NSUTF8StringEncoding)];
    
    UInt8 pwdLength = pwd.length;
    NSData *pwdLengthData = [NSData dataWithBytes:&pwdLength length:sizeof(pwdLength)];
    NSData *pwdData = [pwd dataUsingEncoding:(NSUTF8StringEncoding)];
    
    UInt8 meshId0 = (arc4random() % 254) + 1;
    UInt8 meshId1 = (arc4random() % 254) + 1;
    UInt8 meshId2 = (arc4random() % 254) + 1;
    NSData *meshId0Data = [NSData dataWithBytes:&meshId0 length:sizeof(meshId0)];
    NSData *meshId1Data = [NSData dataWithBytes:&meshId1 length:sizeof(meshId1)];
    NSData *meshId2Data = [NSData dataWithBytes:&meshId2 length:sizeof(meshId2)];
    
    NSMutableData *value = [NSMutableData data];
    [value appendData:meshId0Data];
    [value appendData:meshId1Data];
    [value appendData:meshId2Data];
    [value appendData:ssidLengthData];
    [value appendData:ssidData];
    [value appendData:pwdLengthData];
    [value appendData:pwdData];
    FBtMessageModel *messageModel = [self modelWithSignal:(FBtSig_setNetMS) value:value];
    return messageModel;
}





@end
